﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace linkouqs.Gamekit {

    public sealed class CharacterHealth : MonoBehaviour {

        /// <summary>
        /// 血量改变事件
        /// </summary>
        public event System.Action<int, int> OnHealthChange;

        [Range(1, 500)]
        public int maxHealth = 5;

        /// <summary>
        /// 当前血量
        /// </summary>
        public int CurrentHealth {
            get {
                return _currentHealth;
            }
            set {
                var prev = _currentHealth;

                _currentHealth = Mathf.Clamp(value, 0, maxHealth);

                OnHealthChange?.Invoke(prev, _currentHealth);

            }
        }

        int _currentHealth;

        /// <summary>
        /// 是否满血
        /// </summary>
        public bool IsFullHealth {
            get {
                return CurrentHealth == maxHealth;
            }
        }

        /// <summary>
        /// 是否空血槽
        /// </summary>
        public bool IsEmptyHealth {
            get {
                return CurrentHealth == 0;
            }
        }

        /// <summary>
        /// 受到伤害
        /// </summary>
        /// <returns>角色当前生命值是否为0</returns>
        /// <param name="damage">受到的伤害值</param>
        public bool ApplyDamage(int damage) {

            if (damage < 0) {
                throw new System.Exception("受到的伤害值不能小于0");
            }

            CurrentHealth -= damage;

            return IsEmptyHealth;
        }

        /// <summary>
        /// 恢复生命值
        /// </summary>
        /// <returns>是否满血</returns>
        /// <param name="amount">恢复量</param>
        public bool EarnHealth(int amount) {

            if (amount < 0) {
                throw new System.Exception("恢复的生命值不能小于0");
            }

            CurrentHealth += amount;

            return IsFullHealth;
        }

        /// <summary>
        /// 重置生命值
        /// </summary>
        public void ResetHealth() {
            CurrentHealth = maxHealth;
        }


    }

}

